package cn.zhangxin.pethome.system.menu.controller;

import cn.zhangxin.pethome.annotation.PreAuthorize;
import cn.zhangxin.pethome.system.menu.service.IMenuService;
import cn.zhangxin.pethome.system.menu.domain.Menu;
import cn.zhangxin.pethome.system.menu.query.MenuQuery;
import cn.zhangxin.pethome.utils.AjaxResult;
import cn.zhangxin.pethome.utils.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(value = "菜单的API",description="菜单相关的CRUD功能")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "菜单保存",sn = "save")
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.add(menu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PreAuthorize(name = "菜单删除",sn = "delete")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            menuService.delete(id);
            return AjaxResult.success();
        } catch (BusinessException b){
            return AjaxResult.error().setMsg(b.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("网络异常请联系管理员");
    }
	
    //获取用户
    @PreAuthorize(name = "菜单获得一个数据",sn = "findOne")
    @GetMapping("/{id}")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "菜单获得全部数据",sn = "findAll")
    public List<Menu> list(){

        return menuService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "菜单分页查询数据",sn = "findByQuery")
    public AjaxResult json(@RequestBody MenuQuery query){
        try {
            List<Menu> byQuery = menuService.findByQuery(query);
            Long total = menuService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(total);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    //获得父级菜单
    @ApiOperation(value = "获取父级菜单" )
    @GetMapping("/parent")
    public AjaxResult getParentName(){
        return AjaxResult.success().setData(menuService.getParentName());
    }

    @GetMapping("/tree")
    @ApiOperation(value = "获取菜单树" )
    public List<Menu> getMenuTree(){
        return menuService.getMenuTree();
    }
}
